window.Muni = window.Muni || {};
Muni.Circle = Muni.Circle || {};

var defaultCircleOptions = {
    centerLatitude: 34.25,
    centerLongitude: 45.67,
    radius: 10,
    fillColor: "#FF0000",
    fillOpacity: 0.4,
    strokeColor: "#FF0000",
    strokeOpacity: 1,
    strokeWeight: 4
};

var prepareCircleOptions = function(cfg){
    var options = {};
    $.extend(options, defaultCircleOptions, cfg);
    return options;
};


Muni.Circle.google = (function(){
    var circles = [];

    return {
        addCircle: function(map, cfg){
            var options = prepareCircleOptions(cfg);
            var center = new google.maps.LatLng(options.centerLatitude, options.centerLongitude);
            var gOptions = {
                center: center,
                radius: options.radius,
                fillColor: options.fillColor,
                fillOpacity: options.fillOpacity,
                strokeColor: "#FF0000",
                strokeOpacity: options.strokeOpacity,
                strokeWeight: options.strokeWeight,
                map: map
            };   
            var circle = new google.maps.Circle(gOptions);
            circles.push(circle);
            return circle;
        },

        getCircles: function(){
            return circles;
        },

        removeCircles: function(circle){
            circle.setMap(null);
            var idx = circles.indexOf(circle);
            if(idx != -1){
                circles.splice(idx, 1);
            }
        }
        
    };
})();

Muni.Circle.seznam = (function(){
    var circles = [];
    var layer = null;

    var prepareLayer = function(map){
        if(layer == null){
            layer = new SMap.Layer.Geometry();
        }
        map.addLayer(layer);
        layer.enable();
    }

    return {
        addCircle: function(map, cfg){
            prepareLayer(map);
            var options = prepareCircleOptions(cfg);
            var center = SMap.Coords.fromWGS84(options.centerLatitude, options.centerLongitude);
            var sOptions = {
                color: options.fillColor,
                opacity: options.fillOpacity,
                outlineColor: options.strokeColor,
                outlineOpacity: options.strokeOpacity,
                outlineWidth: options.outlineWidth                
            };
            var circle = layer.addCircle(center, options.radius, sOptions);
            circles.push(circle);
            return circle;            
        },

        getCircles: function(){
            return circles;
        },

        removeCircle: function(circle){
            layer.removeCircle(circle);
            var idx = circles.indexOf(circle);
            if(idx != -1){
                circles.splice(idx, 1);
            }
        }
        
    }
})();

Muni.Circle.bing = (function(){
    var circles = [];
    var map = null;
    var earthRadiusInMeters = 6367000.0;

    var convertToRGB = function(color){
        var withoutHash = color.charAt(0) == '#' ? color.substring(1, 7) : color;
        var r = parseInt(withoutHash.substring(0, 2), 16);
        var g = parseInt(withoutHash.substring(2, 4), 16);
        var b = parseInt(withoutHash.substring(4, 6), 16);
        return [r, g, b];
    };

    var toRadians = function(n) {
        return n * (Math.PI / 180);
    };

    var toDegrees = function(r){
        return r * 180 / Math.PI;
    };

    return {
        addCircle: function(bmap, cfg){
            map = bmap;
            var options = prepareCircleOptions(cfg);
            
            var lat = toRadians(options.centerLatitude);
            var lng = toRadians(options.centerLongitude);
            
            var earthRadius = parseFloat(options.radius) / earthRadiusInMeters;
            var points = [];
            for(i =0; i<360; i++){
                var radians = toRadians(i);

                var latRadians = Math.asin(Math.sin(lat) * Math.cos(earthRadius) + Math.cos(lat) * Math.sin(earthRadius) * Math.cos(radians));
                var lngRadians = lng + Math.atan2(Math.sin(radians) * Math.sin(earthRadius) * Math.cos(lat), Math.cos(earthRadius) - Math.sin(lat) * Math.sin(latRadians));
                points.push(new Microsoft.Maps.Location(toDegrees(latRadians), toDegrees(lngRadians)));                
            }


            var fillRGB = convertToRGB(options.fillColor);
            var fillColor = new Microsoft.Maps.Color(options.fillOpacity, fillRGB[0], fillRGB[1], fillRGB[2]);

            var strokeRGB = convertToRGB(options.strokeColor);
            var strokeColor = new Microsoft.Maps.Color(options.strokeOpacity, strokeRGB[0], strokeRGB[1], strokeRGB[2]);

            var bOptions = {
                strokeColor: strokeColor,
                strokeThickness: options.strokeWeight,
                fillColor: fillColor
            };
            var circle = new Microsoft.Maps.Polygon(points);

            map.entities.push(circle);
            circles.push(circle);
            return circle;
        },

        getCircles: function(){
            return circles;
        },

        removeCircles: function(circle){
            map.entities.remove(circle);
            var idx = circles.indexOf(circle);
            if(idx != -1){
                circles.splice(idx, 1);                
            }            
        }
    };
    
})();
